package org.dromara.travel.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.travel.domain.bo.RiskDisclaimerBo;
import org.dromara.travel.domain.vo.RiskDisclaimerVo;
import org.dromara.travel.domain.RiskDisclaimer;
import org.dromara.travel.mapper.RiskDisclaimerMapper;
import org.dromara.travel.service.IRiskDisclaimerService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 免责声明Service业务层处理
 *
 * @author jacheut
 * @date 2024-04-04
 */
@RequiredArgsConstructor
@Service
public class RiskDisclaimerServiceImpl implements IRiskDisclaimerService {

    private final RiskDisclaimerMapper baseMapper;

    /**
     * 查询免责声明
     */
    @Override
    public RiskDisclaimerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询免责声明列表
     */
    @Override
    public TableDataInfo<RiskDisclaimerVo> queryPageList(RiskDisclaimerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RiskDisclaimer> lqw = buildQueryWrapper(bo);
        Page<RiskDisclaimerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询免责声明列表
     */
    @Override
    public List<RiskDisclaimerVo> queryList(RiskDisclaimerBo bo) {
        LambdaQueryWrapper<RiskDisclaimer> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<RiskDisclaimer> buildQueryWrapper(RiskDisclaimerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RiskDisclaimer> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, RiskDisclaimer::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), RiskDisclaimer::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), RiskDisclaimer::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增免责声明
     */
    @Override
    public Boolean insertByBo(RiskDisclaimerBo bo) {
        RiskDisclaimer add = MapstructUtils.convert(bo, RiskDisclaimer.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改免责声明
     */
    @Override
    public Boolean updateByBo(RiskDisclaimerBo bo) {
        RiskDisclaimer update = MapstructUtils.convert(bo, RiskDisclaimer.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RiskDisclaimer entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除免责声明
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
